package Demo1;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-07-07
 * Time: 15:40
 */
public class MyArrayList implements IList{
    public int[] array;
    public int useSize;
    public static final int ARRAY_CAPACITY = 5;

    public MyArrayList(){
        this.array = new int[ARRAY_CAPACITY];
    }


    @Override
    public void add(int data) {
        //判断数组空间是否满
        if(isFull()){
            //扩容
            this.expansion();
        }
        this.array[useSize++] = data;
    }



    @Override
    public void add(int pos, int data) {

        try{
            //判断pos位置是否合法
            posCheck1(pos);

            //判断数组空间是否满
            if(isFull()){
                //扩容
                this.expansion();
            }
            for (int i = this.useSize - 1; i >= pos; i--) {
                array[i + 1] = array[i];
            }
            this.array[pos] = data;
            this.useSize++;

        }catch(posIllegal e){
            e.printStackTrace();
        }
    }

    public void posCheck1(int pos){
        if(pos < 0 || pos > useSize){
            throw new posIllegal("pos位置不合法");
        }
    }
    // 判定是否包含某个元素
    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < this.useSize; i++) {
            if(array[i] == toFind){
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的位置
    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < this.useSize; i++) {
            if(array[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    public void posCheck2(int pos){
        if(pos < 0 || pos >= useSize){
            throw new posIllegal("pos位置不合法");
        }
    }
    //判断数组有效长度是否为0
    public void arrayEmpty(){
        if(this.useSize == 0){
            throw new ArrayEmpty("数组有效长度为0");
        }
    }
    // 获取 pos 位置的元素
    @Override
    public int get(int pos) {
        //检查pos位置是否合法
        try{
            arrayEmpty();
            posCheck2(pos);
            return this.array[pos];
        }catch(posIllegal e){
            e.printStackTrace();
        }catch(ArrayEmpty e){
            e.printStackTrace();
        }
        return -1;
    }
    // 给 pos 位置的元素设为 value
    @Override
    public void set(int pos, int value) {
        //检查pos位置是否合法
        try{
            arrayEmpty();
            posCheck2(pos);
            this.array[pos] = value;
        }catch(posIllegal e){
            e.printStackTrace();
        }catch(ArrayEmpty e){
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int toRemove) {
        int pos = this.indexOf(toRemove);
        if(pos == -1){
            System.out.println("您要删除的元素不存在");
            return;
        }
        for (int i = pos; i < useSize - 1; i++) {
            array[i] = array[i + 1];
        }
        this.useSize--;
    }
    // 获取顺序表长度
    @Override
    public int size() {
        return this.useSize;
    }

    @Override
    public void clear() {
//        for (int i = 0; i < this.useSize; i++) {
//            array[i] = null;
//        }
        this.useSize = 0;
    }
    // 打印顺序表，注意：该方法并不是顺序表中的方法，为了方便看测试结果给出的
    @Override
    public void display() {
        for (int i = 0; i < this.useSize; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    @Override
    public boolean isFull() {
        return this.array.length == this.useSize;
    }

    //扩容
    private void expansion(){
        this.array = Arrays.copyOf(this.array,2 * array.length);
    }
}
